<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery回调模块结构</title>
    <script src="../assets/libs/jquery-2.0.3.js"></script>
</head>
<body>
    <script>
        /**jQuery callbacks 源码结构
         * 1.Options 参数缓存
         * 2.内部 fire 触发器的设计
         * 3.外部
         */
        /*
        jQuery.Callbacks = function(options) {
            options = typeof options === "string" ? (optionsCache[options] || createOptions(options)) : jQuery.extend({}, options);

            // 触发器
            fire = function() {}

            // 外部方法
            self = {
                add: function() {},
                remove: function() {},
                has: function(fn) {},
                empty: function() {},
                disable: function() {},
                disabled: function() {},
                lock: function() {},
                locked: function() {},
                fireWith: function(context, args) {},
                fire: function() {},
                fired: function() {}
            };
            return self;
        };
        */
    </script>

    <script>
        // 缓存池
        var optionsCache = {}
        // 匹配非空格字符，返回一个数组，相当于按空格分割
        var rnotwhite = /\S+/g

        // 
        function createOptions(options) {
            var object = optionsCache[options] = {}
            console.log('options.match(rnotwhite) :>> ', options.match(rnotwhite)); // ['one', 'memory']
            jQuery.each(options.match(rnotwhite) || [], function(_, flag) {
                object[flag] = true
            })
            return object
        }

        function callback(options) {
            options = typeof options === 'string' ? (optionsCache[options] || createOptions(options)) : jQuery.extend({}, options)
            show(options)
        }

        function show(data) {
            console.log('data :>> ', data);
        }

        callback('once memory')  // {one: true, memory: true}
    </script>
</body>
</html>